// pages/order/index.js
var app = getApp() 
Page( { 
 data: {   
  currentTab: 0,
  scrollTop:0,
  page:0,
  ordersData:[],
  loading:true,
  host: app.d.hostImg,
 }, 
 onLoad: function(options) {
  var current = options.id;
  console.log(current)
  this.data.currentTab = current?current:0
  var systemInfo = wx.getSystemInfoSync()
    this.setData({
      currentTab:this.data.currentTab,
      windowHeight:systemInfo.windowHeight
    })
 },
 onShow:function(options){
  if(this.data.currentTab==0){
    this.setCurrentData()
  }
 },
 setCurrentData:function(){
  if(!this.data.loading){
    return false;
  }
  var self = this;
  wx.request({
      url: app.d.ceshiUrl + '/Api/GroupOrder/index',
      method: 'post',
      data: {
        uid: app.d.userId,
        order_type: self.data.currentTab,
        page: self.data.page,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
    success:function(res){
      if(!res){
        self.data.loading = false
        self.setData({
          loading:false
        })
        var ordersData = self.data.ordersData
      }else{
        var ordersData = self.data.ordersData = self.data.ordersData.concat(res.data.ord)
      }
      console.log(res.data.ord.length)
      if(res.data.ord.length<4){
        self.setData({
          loading:false
        })
      }
      self.setData({
        ordersList:ordersData
      })
    }
  })
 },
 toGroupDetail:function(e){
  var id = e.currentTarget.dataset.id;
  wx.redirectTo({
       url: '../group/detail?gid='+ id
  });
 },
 toPay: function (e) {
    var group_flag = e.currentTarget.dataset.orderid;
    var order_sn = e.currentTarget.dataset.ordersn;
    if (!order_sn) {
      wx.showToast({
        title: "订单异常!",
        duration: 2000,
      });
      return false;
    }
    wx.request({
      url: app.d.ceshiUrl + '/Api/Wxpay/wxpay',
      data: {
        group_flag: group_flag,
        order_sn: order_sn,
        uid: app.d.userId,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }, // 设置请求的 header
      success: function (res) {
        if (res.data.status == 1) {
          var order = res.data.arr;
          wx.requestPayment({
            timeStamp: order.timeStamp,
            nonceStr: order.nonceStr,
            package: order.package,
            signType: 'MD5',
            paySign: order.paySign,
            success: function (res) {
              wx.showToast({
                title: "支付成功!",
                duration: 2000,
              });
              setTimeout(function () {
                var url4='../order_group/index?id=0'
                wx.navigateTo({
                  url: url4
                });
                // wx.navigateTo({
                //   url: '../user/dingdan?currentTab=1&otype=deliver',
                // });
              }, 3000);
            },
            fail: function (res) {
              console.log(res)
              wx.showToast({
                title:'支付取消',
                duration: 3000
              })
            }
          })
        } else {
          wx.showToast({
            title: res.data.err,
            duration: 2000
          });
        }
      },
      fail: function (e) {
        // fail
        wx.showToast({
          title: '网络不给力！',
          duration: 2000
        });
      }
    })
  },
 showOrderDetail:function(e){
  var id = e.currentTarget.dataset.id;
    wx.redirectTo({
       url: '../order_group/detail?oid='+ id
    });
 },
 showGoodsDetial:function(e){
  var id = e.currentTarget.dataset.id;
  app.redirect('goods/detail','gid='+id);
 },
 // 滑动切换tab 
 bindChange: function( e ) { 
  this.data.page = 0
  this.data.ordersData=[]
  this.data.loading =true
  this.data.currentTab = e.detail.current
  this.setCurrentData()
  this.setData({
    loading:true,
    ordersList:[],
    currentTab: this.data.currentTab
  })
 }, 
 // 点击tab切换 
 swichNav: function( e ) {
    if( this.data.currentTab == e.currentTarget.dataset.current ) return;

    this.data.currentTab = e.currentTarget.dataset.current
    this.setData({
      currentTab: this.data.currentTab
    })
    console.log(this.data.currentTab)
 },
 scrolltolower:function(){
  ++this.data.page
  this.setCurrentData()
 },
   //确认收货
recOrder: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.orderId;
    wx.showModal({
      title: '提示',
      content: '你确定已收到宝贝吗？',
      success: function (res) {
        res.confirm && wx.request({
          url: app.d.ceshiUrl + '/Api/GroupOrder/orders_edit',
          method: 'post',
          data: {
            id: orderId,
            type: 'receive',
          },
          header: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            //--init data
            var status = res.data.status;
            if (status == 1) {
              wx.showToast({
                title: '操作成功！',
                duration: 2000
              });
              var url4='../order_group/index?id=5'
                wx.navigateTo({
                  url: url4
              });
            } else {
              wx.showToast({
                title: res.data.err,
                duration: 2000
              });
            }
          },
          fail: function () {
            // fail
            wx.showToast({
              title: '网络不给力！',
              duration: 2000
            });
          }
        });

      }
    });
  },
})